﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using AdRotator.Model;

#if iOS
using MonoTouch.UIKit;
#endif

namespace AdRotatorXNA
{
    public enum AdState
    {
        NotInitialized,
        Initialized,
        AdLoaded,
        AdFailed
    }
    public abstract class AdComponent : DrawableGameComponent
    {
        private AdState _adState = AdState.NotInitialized;
        public AdState State
        {
            get { return _adState;  }
            protected set
            {
                if (value == _adState)
                    return;

                _adState = value;

                switch(_adState)
                {
                    case AdState.AdLoaded:
                        if (AdLoaded != null)
                        {
#if iOS
                            UIApplication.SharedApplication.BeginInvokeOnMainThread(() => AdLoaded(this.AdInfo));
#else
                            AdLoaded(this.AdInfo);
#endif   
                        }
                        break;

                    case AdState.AdFailed:
                        if (AdLoadingFailed != null)
                        {
#if iOS
                            UIApplication.SharedApplication.BeginInvokeOnMainThread(() => AdLoadingFailed(this.AdInfo));
#else
                            AdLoadingFailed(this.AdInfo);
#endif
                            
                        }
                        break;

                    default:
                        break;
                }
            }
        }

        public string ApplicationID;
        public string AdvertisementID;

        public AdInformation AdInfo;

        public delegate void OnAdFailed(AdInformation sender);
        public delegate void OnAdLoaded(AdInformation sender);
        public delegate void OnAdClicked(AdInformation sender);

        public event OnAdFailed AdLoadingFailed;
        public event OnAdLoaded AdLoaded;
        public event OnAdClicked AdClicked;

        public bool IsValid;

        protected AdComponent(Game game)
            : base(game)
        {
        }

        public virtual void Initialize(Game _game) { }

        public abstract void UpdatePosition(Vector2 newPos);
        public abstract void UpdateAd(AdInformation adInfo);

        protected override void UnloadContent()
        {
            ClearEventHandlers();

            base.UnloadContent();
        }

        protected override void OnEnabledChanged(object sender, EventArgs args)
        {
            if (!Enabled)
                ClearEventHandlers();

            base.OnEnabledChanged(sender, args);
        }

        private void ClearEventHandlers()
        {
            _adState = AdState.NotInitialized;

            AdLoadingFailed = null;
            AdLoaded = null;
            AdClicked = null;
        }

        protected void Clicked()
        {
            if (AdClicked != null)
                AdClicked(this.AdInfo);
        }
    }
}
